package csdn;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class CopyPicture {
    public static void main(String[] args) {
        String srcFilepath="D:\\javacode\\javacode\\IO_1\\mysql_1.jpg";//源路径
        String destFilepath="D:\\javacode\\javacode\\IO_1\\mysql_2.jpg";//拷贝路径
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;

        try {
            fileInputStream=new FileInputStream(srcFilepath);
            fileOutputStream=new FileOutputStream(destFilepath);

            //定义一个字节数组,提高读取效果
            byte[] buf = new byte[1024];
            int readLen = 0;
            while ((readLen = fileInputStream.read(buf)) !=-1) {
                //读取到后，就写入到文件 通过 fileOutputStream
                //即，是一边读，一边写
                fileOutputStream.write(buf, 0, readLen);//一定要使用这个方法
            }
            System.out.println("拷贝 ok~");

        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            //关闭输入流和输出流，释放资源
            try {
                if(fileInputStream!=null) {
                    fileInputStream.close();
                }
                if(fileOutputStream!=null) {
                    fileOutputStream.close();
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }


    }
}
